
/**
 * nobitterwindow.h
 * 
 * NobitterWindow is the main window of nobitter
 * 
 * Copyright (c) 2008 Harunobu Oyama <nobukichi at gmail.com>
 * Licensed under GNU GPL version 3
 *
 */

#ifndef		NOBITTER_WINDOW_INCLUDED
#define		NOBITTER_WINDOW_INCLUDED

#ifdef Q_WS_WIN
#include	<Windows.h>
#endif

#include	<QMainWindow>
#include	<QSystemTrayIcon>
#include	"tweet.h"
#include	"ui_mainwindow.h"

class NobitterWindow : public QMainWindow, private Ui::MainWindow
{
	Q_OBJECT
public:
	NobitterWindow(QWidget* parent = 0, Qt::WindowFlags flags = 0);
	~NobitterWindow();

    virtual void setVisible(bool visible);
	
	//! toggle visibility of the main window (called from hot key handler)
	void toggleVisibility();
	
public slots:
	//! disable line edit, send button, and update button
	//! enable line edit, send button, and update button
	void slotDisableInput();

	//! it is safer to do so while waiting for Twitter API result
	void slotEnableInput();
	
	//! restore the window and bring it on top of other applications
	void slotRestoreWindow();

signals:
	//! signal emitted when some text is typed in the line edit
	void signalNewMessageSent(QString newMessage);

protected:
    void closeEvent(QCloseEvent* event);

#ifdef Q_WS_WIN
	bool winEvent(MSG* message, long* result);
	unsigned short nobitterHotKey;
#endif

private slots:
	//! send button handler
	void slotSendButtonClicked();

	//! task tray icon click handler
	void slotToggleVisibility(QSystemTrayIcon::ActivationReason reason);

	//! update the background color of line edit when typing plain text while group encryption is in use
	//! white, when typing encrypted text for the group or group encryption is not in use
	//! red, when typing plain text to non-group user while group encryption is in use
	void slotUpdateLineEditColor(const QString& newText);

	//! show nobitter project page
	void slotShowProjectPage();

	//! open the url which appears in the specified row of the model
	void slotOpenUrl(const QModelIndex& index);

	//! show balloon text from task tray icon, if new tweets from other members are stored in the model
	void slotUpdateNewTweetBalloon();

	//! adjust the column width of the main view
	void slotAdjustColumnWidth();

private:
	void setupTrayIcon();
	void setupNewTweetTimer();
	void setupMainView();
	void setupWhenFilter();
	void setupDockMenu();
	void connectActionSignals();
	void connectGuiSignals();
	void connectControllerSignals();

	void updateWhoFilter();
	void updateAtFilter();

	//! update @user completer set to the line edit
	void updateCompleter();

	//! extract a url included in the specified row of the model, if any
	QUrl extractUrl(const QModelIndex& index);

	//! update task tray icon to nobitter icon
	void setDefaultIcon();

	//! update task tray icon to the most recent user's icon
	void setUserIcon(const QString& user);

	//! @user completer
	class QCompleter* friendCompleter;

	//! the controller of nobitter application
	class NobitterControl* pControl;

	//! action to store the main window to the task tray icon
    class QAction* actionMinimize;

	//! action to restore the main window from the task tray icon
    class QAction* actionRestore;

	//! task tray icon
    QSystemTrayIcon* trayIcon;

	//! Tweet used for new message checking.
	Tweet latestTweet;

	//! Windows does not close the balloon text even if we set some timeout
	//! We need to hide and show the task tray icon in order to hide the balloon text.
	//! this flag is used for that purpose.
	bool isShowingNewTweet;

	//! polling timer to check the arrival of new tweet of other group members
	QTimer* pNewTweetTimer;
};

#endif	//	NOBITTER_WINDOW_INCLUDED

